package organization

import (
	"MembershipRegist/db"
	"MembershipRegist/models"
	"time"
)

////////////////////////////////////////////
func (c *Controller) Members() {
	offset := c.GetString("offset")
	limit := c.GetString("limit")
	var members []models.OrganizationUser
	if limit == "" || limit == "0" {
		db.DB.Where("organization_id = ?", c.OrganizationAdmin.OrganizationID).Preload("User").Find(&members)
	} else {
		db.DB.Where("organization_id = ?", c.OrganizationAdmin.OrganizationID).Preload("User").
			Limit(limit).Offset(offset).Find(&members)
	}

	var total int
	db.DB.Model(&models.OrganizationUser{}).Count(&total)
	result := make(map[string]interface{})
	result["status"] = 200
	result["message"] = "success"
	result["total"] = total
	result["members"] = members
	c.Data["json"] = result
	c.ServeJSON()
}

func (c *Controller) MemberInsert() {
	userID, err := c.GetInt("user_id")
	if err != nil {
		c.ResponseFailed(201, "user_id 不合法")
		return
	}

	if value := db.DB.Where("id = ?", userID).First(&models.User{}); value.Error != nil {
		c.ResponseFailed(202, "此用户不存在")
		return
	}

	organizationUser := models.OrganizationUser{
		OrganizationID: c.OrganizationAdmin.OrganizationID,
		UserID:         uint(userID),
		CreatedAt:      time.Now(),
		UpdatedAt:      time.Now(),
		Status:         models.OrganizationUserMember,
	}

	if value := db.DB.Create(&organizationUser); value.Error != nil {
		c.ResponseFailed(203, "添加社团成员失败")
		return
	}

	c.ResponseSuccess()
}

// todo: 硬删除
func (c *Controller) MemberDelete() {
	userID, err := c.GetInt("user_id")
	if err != nil {
		c.ResponseFailed(201, "user_id 不合法")
		return
	}

	if value := db.DB.Unscoped().Delete(&models.OrganizationUser{},
		"organization_id = ? and user_id = ?", c.OrganizationAdmin.OrganizationID, userID); value.Error != nil {
		c.ResponseFailed(202, "删除会员失败")
		return
	}

	c.ResponseSuccess()
}

func (c *Controller) MemberVerification() {
	userID, err := c.GetInt("user_id")
	if err != nil {
		c.ResponseFailed(201, "user_id 不合法")
		return
	}
	status, err := c.GetInt("status")
	if err != nil {
		c.ResponseFailed(201, "status 不合法")
		return
	}
	if status < models.OrganizationUserRegister || status > models.OrganizationUserBan {
		c.ResponseFailed(202, "status 错误")
		return
	}

	db.DB.Model(&models.OrganizationUser{}).
		Where("organization_id = ? and user_id = ?", c.OrganizationAdmin.OrganizationID, userID).
		Update("status", status)

	c.ResponseSuccess()
}

func (c *Controller) MemberQuery() {
	userID, err := c.GetInt("user_id")
	if err != nil {
		c.ResponseFailed(201, "user_id 不合法")
		return
	}

	var organizationUser models.OrganizationUser
	if value := db.DB.Where("organization_id = ? and user_id = ?", c.OrganizationAdmin.OrganizationID, userID).
		Preload("User").
		First(&organizationUser); value.Error != nil {
		c.ResponseFailed(202, "无此用户")
		return
	}
	result := make(map[string]interface{})
	result["status"] = 200
	result["message"] = "success"
	result["organization_user"] = organizationUser
	c.Data["json"] = result
	c.ServeJSON()
}
